package cn.pugle.json.jackson;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.junit.Test;

import java.io.IOException;

/**
 * 插入固定值
 *
 * @author tzp
 * @since 2018/9/20
 */
@Data
public class TestInject {
    String testName;
    @JacksonInject
    Dog dog;

    @Test
    public void test() throws IOException {
        Dog dog = new Dog();
        dog.setName("doggie");
        dog.setAge(1);

        //但是只能是固定的key是class或string
        InjectableValues inject = new InjectableValues.Std().addValue(Dog.class, dog);

        String json = "{\"testName\":\"My bean\"}";
        TestInject o = new ObjectMapper().reader(inject)
                .forType(TestInject.class)
                .readValue(json);
        System.out.println(o);
    }
}
